package com.miguo.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.miguo.framework.utils.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * TimeUtils
 */
@SuppressLint("SimpleDateFormat")
public class DateUtils {

    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat(
            "yyyy-MM-dd");

    public static final SimpleDateFormat simpleDayDateFormat = new SimpleDateFormat("yyyyMMdd");

    /**
     * 时间日期格式化到年月日时分秒.
     */
    public static final String dateFormatYMDHMS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间日期格式化到年月日.
     */
    public static final String dateFormatYMD = "yyyy-MM-dd";

    /**
     * 时间日期格式化到年月.
     */
    public static final String dateFormatYM = "yyyy-MM";

    /**
     * 时间日期格式化到年月日时分.
     */
    public static final String dateFormatYMDHM = "yyyy-MM-dd HH:mm";

    /**
     * 时间日期格式化到月日.
     */
    public static final String dateFormatMD = "MM/dd";

    /**
     * 时分秒.
     */
    public static final String dateFormatHMS = "HH:mm:ss";

    /**
     * 时分.
     */
    public static final String dateFormatHM = "HH:mm";

    /**
     * 上午.
     */
    public static final String AM = "AM";

    /**
     * 下午.
     */
    public static final String PM = "PM";

    public static int getStartTime(Long StartTime) {
        int result = getStartTime(String.valueOf(StartTime));
        return result;
    }

    public static int getStartTime(String StartTime) {
        if (StringUtils.isEmpty(StartTime) == false) {
            String TimeDate = getCurrentDate("yyyyMMddHHmm");
            String StartDate = getStrTime(StartTime, "yyyyMMddHHmm");
            long Time = Long.valueOf(TimeDate);
            long Start = Long.valueOf(StartDate);
            if (Time == Start) {
                return 1;
            } else if (Time < Start) {
                return 2;
            } else if (Time > Start) {
                return 3;
            }
        }
        return 0;
    }

    public static boolean getNewTime(String Time) {
        long Times = Long.valueOf(Time);
        // startsWith equals
        if (DATE_FORMAT_DATE.format(new Date(Times * 1000L)).equals(
                DATE_FORMAT_DATE.format(new Date()))) {
            return true;
        } else {
            return false;
        }
    }

    /* 10位时间截转13位 */
    public static long getTenOrThirteen(String time) {
        int SorItime = Integer.parseInt(time);
        int Itime = SorItime * 1000;
        long Time = Long.valueOf(Itime);
        return Time;
    }

    public static String getStrTime(Long string, String format) {
        String result = getStrTime(String.valueOf(string), format);
        return result;
    }

    // 将时间戳转为字符串
    public static String getStrTime(String string, String format) {
        String Time = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long lcc_time = Long.valueOf(string);
        Time = sdf.format(new Date(lcc_time * 1000L));
        return Time;
    }

    /*
     * 将字符串转为时间戳
     */
    public static long getTime() {
        long re_time;
        long currentTime = System.currentTimeMillis();
        String str = String.valueOf(currentTime);
        if (str.length() < 10) {
            for (int i = str.length(); i < 10; i++) {
                str = str + i;
            }
            re_time = Long.valueOf(str);
        } else {
            re_time = Long.valueOf(str.substring(0, 10));
        }
        return re_time;
    }

    /*
     * 将时间戳转为字符串 ，格式：yyyy.MM.dd 星期几
     */
    public static String getSection(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd  EEEE");
        // 对于创建SimpleDateFormat传入的参数：EEEE代表星期，如“星期四”；MMMM代表中文月份，如“十一月”；MM代表月份，如“11”；
        // yyyy代表年份，如“2010”；dd代表天，如“25”
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    /**
     * 描述：String类型的日期时间转化为Date类型.
     *
     * @param strDate String形式的日期时间
     * @param format  格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return Date Date类型日期时间
     */
    public static Date getDateByFormat(String strDate, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = mSimpleDateFormat.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 描述：Date类型转化为String类型.
     *
     * @param date   the date
     * @param format the format
     * @return String String类型日期时间
     */
    public static String getStringByFormat(Date date, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        String strDate = null;
        try {
            strDate = mSimpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }

    /**
     * 描述：获取表示当前日期时间的字符串.
     *
     * @param format 格式化字符串，如："yyyy-MM-dd HH:mm:ss"
     * @return String String类型的当前日期时间
     */
    public static String getCurrentDate(String format) {
        String curDateTime = getCurrentDates(format);
        return curDateTime;
    }

    public static Long getCurrentDateLong(String format) {
        String curDateTime = getCurrentDates(format);
        long time = Long.parseLong(curDateTime);
        return time;
    }

    public static String getCurrentDates(String format) {
        String curDateTime = null;
        try {
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            Calendar c = new GregorianCalendar();
            curDateTime = mSimpleDateFormat.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return curDateTime;
    }

    public static boolean isCurrentDay(String timeSource) {
        try {
            if ((!TextUtils.isEmpty(timeSource)) && (!"-1".equals(timeSource))) {
                return simpleDayDateFormat.format(Long.valueOf(new Date().getTime())).equals(simpleDayDateFormat.format(new Date(Long.parseLong(timeSource))));
            }
        } catch (Exception e) {
            Log.w(e.getMessage());
        }
        return false;
    }

    /**
     * 描述：计算两个日期所差的天数.
     *
     * @param date1 第一个时间的毫秒表示
     * @param date2 第二个时间的毫秒表示
     * @return int 所差的天数
     */
    public static int getOffectDay(long date1, long date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(date2);
        // 先判断是否同年
        int y1 = calendar1.get(Calendar.YEAR);
        int y2 = calendar2.get(Calendar.YEAR);
        int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
        int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
        int maxDays = 0;
        int day = 0;
        if (y1 - y2 > 0) {
            maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR);
            day = d1 - d2 + maxDays;
        } else if (y1 - y2 < 0) {
            maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
            day = d1 - d2 - maxDays;
        } else {
            day = d1 - d2;
        }
        return day;
    }

}